<template>
  <div class="pt-24px">
    <n-form ref="formRef" :model="model" :rules="rules" :show-label="false" size="large">
      <n-form-item path="phone">
        <n-input v-model:value="model.phone" placeholder="手机号码" />
      </n-form-item>
      <n-form-item path="code">
        <n-input v-model:value="model.code" placeholder="验证码" />
        <div class="w-18px"></div>
        <n-button :disabled="isCounting" @click="handleSmsCode">{{ label }}</n-button>
      </n-form-item>
      <n-form-item path="pwd">
        <n-input v-model:value="model.pwd" type="password" placeholder="密码" />
      </n-form-item>
      <n-form-item path="confirmPwd">
        <n-input v-model:value="model.confirmPwd" type="password" placeholder="确认密码" />
      </n-form-item>
      <n-space :vertical="true">
        <n-button size="large" type="primary" :block="true" :round="true" @click="handleSubmit"
          >确定</n-button
        >
        <n-button size="large" block="true" :round="true" @click="toLogin('pwd-login')"
          >返回</n-button
        >
      </n-space>
    </n-form>
  </div>
</template>

<script setup lang="ts">
import { useRouterChange, useSmsCode } from '@/hooks'
import { FormInst, useMessage } from 'naive-ui'
import { reactive, ref } from 'vue'

const formRef = ref<(HTMLElement & FormInst) | null>(null)
const model = reactive({
  phone: '',
  code: '',
  pwd: '',
  confirmPwd: '',
})
const getRequiredRule = (message: string) => ({
  required: true,
  trigger: ['blur', 'input'],
  message,
})
const rules = {
  phone: getRequiredRule('请输入手机号'),
  code: getRequiredRule('请输入验证码'),
  pwd: getRequiredRule('请输入密码'),
  confirmPwd: getRequiredRule('请确认密码'),
}
const { start, label, isCounting } = useSmsCode()
const { toLogin } = useRouterChange()
const message = useMessage()

function handleSmsCode() {
  start()
}
function handleSubmit() {
  formRef.value?.validate((error) => {
    if (!error) {
      message.success('验证成功')
    } else {
      message.error('验证失败')
    }
  })
}
</script>

<style scoped></style>
